/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T005T007;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T007Illness;
import ep.persistence.entities.T005Patient;

/**
 *
 * @author Spyros
 */
public class T005T007JpaController {

    public T005T007JpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T005T007 t005T007) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T007Illness t007Illness = t005T007.getT007Illness();
            if (t007Illness != null) {
                t007Illness = em.getReference(t007Illness.getClass(), t007Illness.getId());
                t005T007.setT007Illness(t007Illness);
            }
            T005Patient t005Patient = t005T007.getT005Patient();
            if (t005Patient != null) {
                t005Patient = em.getReference(t005Patient.getClass(), t005Patient.getId());
                t005T007.setT005Patient(t005Patient);
            }
            em.persist(t005T007);
            if (t007Illness != null) {
                t007Illness.getT005T007Collection().add(t005T007);
                t007Illness = em.merge(t007Illness);
            }
            if (t005Patient != null) {
                t005Patient.getT005T007Collection().add(t005T007);
                t005Patient = em.merge(t005Patient);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T005T007 t005T007) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T005T007 persistentT005T007 = em.find(T005T007.class, t005T007.getId());
            T007Illness t007IllnessOld = persistentT005T007.getT007Illness();
            T007Illness t007IllnessNew = t005T007.getT007Illness();
            T005Patient t005PatientOld = persistentT005T007.getT005Patient();
            T005Patient t005PatientNew = t005T007.getT005Patient();
            if (t007IllnessNew != null) {
                t007IllnessNew = em.getReference(t007IllnessNew.getClass(), t007IllnessNew.getId());
                t005T007.setT007Illness(t007IllnessNew);
            }
            if (t005PatientNew != null) {
                t005PatientNew = em.getReference(t005PatientNew.getClass(), t005PatientNew.getId());
                t005T007.setT005Patient(t005PatientNew);
            }
            t005T007 = em.merge(t005T007);
            if (t007IllnessOld != null && !t007IllnessOld.equals(t007IllnessNew)) {
                t007IllnessOld.getT005T007Collection().remove(t005T007);
                t007IllnessOld = em.merge(t007IllnessOld);
            }
            if (t007IllnessNew != null && !t007IllnessNew.equals(t007IllnessOld)) {
                t007IllnessNew.getT005T007Collection().add(t005T007);
                t007IllnessNew = em.merge(t007IllnessNew);
            }
            if (t005PatientOld != null && !t005PatientOld.equals(t005PatientNew)) {
                t005PatientOld.getT005T007Collection().remove(t005T007);
                t005PatientOld = em.merge(t005PatientOld);
            }
            if (t005PatientNew != null && !t005PatientNew.equals(t005PatientOld)) {
                t005PatientNew.getT005T007Collection().add(t005T007);
                t005PatientNew = em.merge(t005PatientNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t005T007.getId();
                if (findT005T007(id) == null) {
                    throw new NonexistentEntityException("The t005T007 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T005T007 t005T007;
            try {
                t005T007 = em.getReference(T005T007.class, id);
                t005T007.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t005T007 with id " + id + " no longer exists.", enfe);
            }
            T007Illness t007Illness = t005T007.getT007Illness();
            if (t007Illness != null) {
                t007Illness.getT005T007Collection().remove(t005T007);
                t007Illness = em.merge(t007Illness);
            }
            T005Patient t005Patient = t005T007.getT005Patient();
            if (t005Patient != null) {
                t005Patient.getT005T007Collection().remove(t005T007);
                t005Patient = em.merge(t005Patient);
            }
            em.remove(t005T007);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T005T007> findT005T007Entities() {
        return findT005T007Entities(true, -1, -1);
    }

    public List<T005T007> findT005T007Entities(int maxResults, int firstResult) {
        return findT005T007Entities(false, maxResults, firstResult);
    }

    private List<T005T007> findT005T007Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T005T007.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T005T007 findT005T007(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T005T007.class, id);
        } finally {
            em.close();
        }
    }

    public int getT005T007Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T005T007> rt = cq.from(T005T007.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
